package com.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;


public class KtDao {//增删改查的和数据库连接  Dao数据库访问
    //定义四个类变量变量名static修饰
    private static String dirver;
    private static String url;
    private static String user;
    private static String password;

//    static {
//        Properties prop = new Properties();
//        String path = KtDao.class.getProtectionDomain().getCodeSource().getLocation().getPath()
//                + "/com/babershop/dao/db.properties";
//        System.out.println(path);
//        //  FileReader fileReader = null;//FileReader读纯文本
//        try (FileReader fileReader = new FileReader(path)) {//就不需要要写finally
//            //       input=new FileInputStream("config.properties");
//            prop.load(fileReader);
//            dirver = prop.getProperty("dirver");
//            url = prop.getProperty("url");
//            user = prop.getProperty("user");
//            password = prop.getProperty("password");
//
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }
    //finally{
//            if(input!=null){
//                try {
//                    input.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }}





    public static boolean isExist(String sql) {
        try (Connection con = 和数据库建立连接()) {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            return rs.next();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return false;
    }

    public static boolean 我能执行DDL和DML中的insertdeleteupdate(String sql) {
        try (Connection con = 和数据库建立连接()) {
            Statement st = con.createStatement();
            return st.executeUpdate(sql) > 0;//执行更新
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {

        }
        return false;
    }

    private static Connection 和数据库建立连接() {
        Connection connection = null;
        try {
            //   Class.forName(dirver);
//            Class.forName("oracle.jdbc.driver.OracleDriver");

            // Class.forName("java.lang.String2");
//             connection= DriverManager.getConnection("jdbc:oracle:thin:@192.168.79.131:1521:orcl","scott","ROOT");//orcale/连接
            connection= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/babershop?serverTimezone=UTC","root","root");
            //    connection = DriverManager.getConnection(url, user, password);

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return connection;
    }

    public static void main(String[] args) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            // Class.forName("java.lang.String2");
            Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.79.131:1521:orcl", "scott", "ROOT");
            Statement statement = connection.createStatement();//statement接口 Statement通常指代用于执行静态语句的对象。
//            statement.executeUpdate("create table zsls(a int ,b float ,c char )");//
//          statement.executeUpdate("insert into zsls values('30000','2000','a')");
            ResultSet rs = statement.executeQuery("select*from aaa where b like '%bc'");
            while (rs.next()) {
                //System.out.println(rs.getInt("a")+rs.getFloat("b")+rs.getString("c"));//数据库中char、varchar在java是Strig
                System.out.println(rs.getString("a") + rs.getString("b"));
            }
//            System.out.println("按任意键结束");
//            System.in.read();//从键盘输入
            Scanner scan = new Scanner(System.in);

            System.out.println("SQL>");
            while (scan.hasNext()) {//hasNext() 是 Java 中 Scanner 类的一个方法，用于检查输入流中是否还有下一个标记（token）。标记是以空格、制表符或换行符为分隔符的一个单词

                String s = scan.nextLine();//它将读取输入流中的下一行文本，直到遇到换行符（\n）为止

                if (s.equals("exit")) break;
                try {
                    statement.executeUpdate(s);
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                //写可以执行ddl: create alter drop+dml中的insert delete update select
                // System.out.println(s);
                System.out.println("SQL>");
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());

        } finally {
            System.out.println(2);

        }
    }

}
